/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.schedule.img;

import hope.platform.data.domain.DataManagement;
import hope.platform.data.domain.SiteDistance;
import hope.platform.data.domain.SiteInfo;
import hope.platform.data.domain.entity.ObservedData;
import hope.platform.data.server.data.header.DataManagementService;
import hope.platform.data.server.data.header.SiteDistanceService;
import hope.platform.data.server.data.header.SiteInfoService;
import hope.platform.data.server.remote.ObservedService;
import hope.platform.data.server.schedule.header.CheckObservedDataService;
import hope.platform.data.utils.TimeUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/27 11:08
 */
//@Service
public class CheckObservedDataServiceImg implements CheckObservedDataService {

    @Autowired
    private SiteInfoService siteInfoService;

    @Autowired
    private SiteDistanceService siteDistanceService;

    @Autowired
    private DataManagementService dataManagementService;

    @Autowired
    private ObservedService observedService;

    private Date beginDate;

    private Date endDate;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private List<DataManagement> dataManagements;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class DataUnit {
        Double value;
        Double increment;
    }

    @Override
    public void check() {
        endDate = TimeUtils.getBeforeTime(new Date(), 5);
        beginDate = TimeUtils.getBeforeTime(endDate, 1);

        //获取所有的站点信息。
        List<SiteInfo> siteInfos = siteInfoService.getAllSiteInfo();

        //获取所有的字段信息
        dataManagements = dataManagementService.getAll();

        Map<String, Map<String, DataUnit>> allData = new HashMap<>();

        for (SiteInfo siteInfo : siteInfos) {

            allData.clear();

            //遍历每个站点，查询该站点300公里范围内的周围站点。
            List<SiteDistance> distances = siteDistanceService.getBySerialNumberAndDistanceBetween(
                    siteInfo.getSerialNumber(), 0.0, 100.0);
            //如果少于3个站点就不做分析
            if (distances.size() < 3) {
                continue;
            }
            //依次获取自己以及周围站点的数据存放在allData中。
            //首先加入自己的数据
            getSingleData(allData, siteInfo.getSerialNumber());
            //加入相邻站点的数据
            for (SiteDistance siteDistance : distances) {
                getSingleData(allData, siteDistance.getOtherSerialNumber());
            }
            //再次做判断所有数据是否超过三个，如果没有超过就可以直接跳过看下一个了。
            if (allData.size() < 3) {
                continue;
            }

            //判断是否异常(机制)
            //异常报警
        }


    }

    /**
     * 获得一个站点的所有数据信息，根据字段名称来。
     */
    public void getSingleData(Map<String, Map<String, DataUnit>> allData, String serialNumber) {
        //首先存放自己
        Map<String, DataUnit> singleData = new HashMap<>();
        //依次放入14种数据
        for (DataManagement dataManagement : dataManagements) {
            //查询该字段的信息
            List<ObservedData> datas = observedService.getBySerialNumberAndDataNameAndGmtCreateBetween(
                    serialNumber, dataManagement.getDataName(),
                    dateFormat.format(beginDate), dateFormat.format(endDate));
            //假如数据没有，就跳过取下一条。
            if (datas.size() <= 0) {
                continue;
            }
            //这里将默认舍弃其他数据，只就近取一条数据。
            ObservedData observedData = datas.get(datas.size() - 1);
            DataUnit dataUnit = new DataUnit(observedData.getDataValue(), observedData.getDataIncrement());
            singleData.put(dataManagement.getDataName(), dataUnit);
        }
        if (singleData.size() > 0) {
            allData.put(serialNumber, singleData);
        }
    }
}
